// API工具类
import config from './config.js'

export default {
  // 用户登录/注册
  loginOrRegister(username) {
    return uni.request({
      url: `${config.BASE_URL}/users/login`,
      method: 'POST',
      data: { username },
      header: {
        'Content-Type': 'application/json'
      }
    })
  },

  // 获取用户列表
  getUserList() {
    return uni.request({
      url: `${config.BASE_URL}/users/list`,
      method: 'GET'
    })
  },

  // 发送消息
  sendMessage(message) {
    return uni.request({
      url: `${config.BASE_URL}/messages/send`,
      method: 'POST',
      data: message,
      header: {
        'Content-Type': 'application/json'
      }
    })
  },

  // 获取聊天记录
  getChatMessages(userId, otherUserId) {
    return uni.request({
      url: `${config.BASE_URL}/messages/chat/${userId}/${otherUserId}`,
      method: 'GET'
    })
  },

  // 上传图片
  uploadImage(filePath) {
    return new Promise((resolve, reject) => {
      uni.uploadFile({
        url: `${config.BASE_URL}/files/upload`,
        filePath: filePath,
        name: 'file',
        success: (res) => {
          try {
            const data = JSON.parse(res.data)
            resolve(data)
          } catch (e) {
            reject(e)
          }
        },
        fail: (err) => {
          reject(err)
        }
      })
    })
  }
}

